package tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TCPClient {
    private Socket socket;
    public TCPClient(String host, int port) throws IOException {
        socket = new Socket(host, port);
    }
    public void start() {
        System.out.println("客户端启动：");
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()) {
            Scanner in = new Scanner(System.in);
            Scanner res = new Scanner(inputStream);

            PrintWriter printWriter = new PrintWriter(outputStream);
            while(true) {
                System.out.print(">");
                String request = in.nextLine();
                //发送请求
                printWriter.println(request);
                printWriter.flush();
                //接收响应
                String response = res.nextLine();
                System.out.println(">" + response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        TCPClient client = new TCPClient("127.0.0.1", 8090);
        client.start();
    }
}
